package dk.lector;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import dk.lector.domain.TeamShareMobile;
import dk.lector.domain.entities.User;


public class SplashScreenActivity extends Activity {
	private String TAG = this.getClass().getSimpleName();

	private BroadcastReceiver onStartupReceiver;
	private boolean initialized;
	private boolean configured;
	private User currentUser;
	private long lastUpdate;
	private boolean closed = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

	
		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.splashscreen_layout);	
		final TextView loadingText = (TextView) findViewById(R.id.loading_textview);
		final Animation blinkanimation = AnimationUtils.loadAnimation(this, R.anim.blink_animation);
		loadingText.startAnimation(blinkanimation);
		TeamShareMobile core = (TeamShareMobile) getApplicationContext();
		   Handler handler = new Handler();
		    handler.postDelayed(new Runnable() {
		      public void run() {
		    	  if(!closed)
		    	  forwardToHomeScreen();
		      }
		    }, 3000);
		
		 
		
		
	
		onStartupReceiver = new BroadcastReceiver() {
			@Override
			
			public void onReceive(Context context, Intent intent) {
				
				variablesFromBundle(((TeamShareMobile) context.getApplicationContext()).getState());
				forwardToHomeScreen();
			}
		};
		if(core.isInitialized())forwardToHomeScreen();
	}
	
	private void variablesFromBundle(Bundle globalState)
	{
	currentUser = (User) globalState.get("currentUser");
	configured = globalState.getBoolean("configured");
	initialized = globalState.getBoolean("initialized");
	lastUpdate = globalState.getLong("lastUpdate");
		
	}
	
	private Bundle variablesToBundle()
	{
		Bundle state = new Bundle();
		state.putParcelable("currentUser", currentUser);
		state.putBoolean("configured", configured);
		state.putBoolean("initialized", initialized);
		state.putLong("lastUpdate", lastUpdate);
		return state;
		
	};
	
	private void forwardToHomeScreen(){
		Intent homeActivity = new Intent(SplashScreenActivity.this, DashBoardActivity.class);
		homeActivity.putExtra("globals", variablesToBundle());
		startActivity(homeActivity);
		closed = true;
		finish();	

	};
	
	@Override
	protected void onResume() {
		Log.d(TAG, "onResume()");
		if(initialized)forwardToHomeScreen();
		super.onResume();
	}
	@Override
	protected void onPause() {
		unregisterReceiver(onStartupReceiver);
		super.onPause();
	}
	@Override
	protected void onStart() {
		Log.d(TAG, "onStart()");
		registerReceiver(onStartupReceiver, new IntentFilter(getString(R.string.ACTION_STARTUP_COMPLETE)));
		super.onStart();
	}

}
